package com.qst.crop.service.impl;

import com.qst.crop.entity.Reserve;
import com.qst.crop.dao.ReserveDao;
import com.qst.crop.service.ReserveService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * (Reserve)表服务实现类
 *
 * @author Wlj
 * @since 2025-09-02 15:25:12
 */
@Service("reserveService")
public class ReserveServiceImpl implements ReserveService {
    @Resource
    private ReserveDao reserveDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Reserve queryById(Integer id) {
        return this.reserveDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param reserve 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Reserve> queryByPage(Reserve reserve, PageRequest pageRequest) {
        long total = this.reserveDao.count(reserve);
        return new PageImpl<>(this.reserveDao.queryAllByLimit(reserve, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param reserve 实例对象
     * @return 实例对象
     */
    @Override
    public Reserve insert(Reserve reserve) {
        this.reserveDao.insert(reserve);
        return reserve;
    }

    /**
     * 修改数据
     *
     * @param reserve 实例对象
     * @return 实例对象
     */
    @Override
    public Reserve update(Reserve reserve) {
        this.reserveDao.update(reserve);
        return this.queryById(reserve.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.reserveDao.deleteById(id) > 0;
    }

    /**
     * 查询个人预约
     *
     * @param questioner 咨询者
     * @return 预约列表
     */
    @Override
    public List<Reserve> selectByQuestioner(String questioner) {
        return this.reserveDao.selectByQuestioner(questioner);
    }

    /**
     * 根据ID修改预约
     *
     * @param reserve 预约实体
     * @return 修改后的预约
     */
    @Override
    public Reserve updateById(Reserve reserve) {
        this.reserveDao.updateById(reserve);
        return this.queryById(reserve.getId());
    }

    /**
     * 查询所有预约
     *
     * @return 预约列表
     */
    @Override
    public List<Reserve> selectAll() {
        return this.reserveDao.selectAll();
    }
}
